package com.head.firtst.ch2.demo1;

import java.util.ArrayList;

/**
 * @author guohuixin
 * @date 2018/8/11
 */
public class WeatherData implements Subject {
    private ArrayList observers;
    private float temp;
    private float humidity;
    private float pressure;

    public WeatherData() {
        observers = new ArrayList();
    }
    public void registerObserver(Observer s) {
        observers.add(s);
    }

    public void removeObserver(Observer s) {
        int i = observers.indexOf(s);
        if (i >= 0) {
            observers.remove(i);
        }
    }

    public void notifyObservers() {
        for(int i=0; i< observers.size(); i++){
            Observer observer = (Observer)observers.get(i);
            observer.update(temp, humidity, pressure);
        }
    }

    public void measurementsChanged(){
        notifyObservers();
    }

    public void setMeasureMents(float temp, float humidity, float pressure) {
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}
